package org.batatasframework.chart;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import org.batatasframework.chart.columns.ChartColumn;
import org.batatasframework.chart.columns.ChartColumnAxis;
import org.batatasframework.chart.columns.ChartColumnValue;
import org.batatasframework.chart.graphics.ChartGraphic;
import org.batatasframework.chart.graphics.ChartGraphicAxis;
import org.batatasframework.chart.graphics.ChartGraphicType;
import org.batatasframework.support.AbstractSupportDefinition;

/**
 * <pre>查询统计的定义</pre>
 * @author jackson
 *
 */
public class ChartDefinition extends AbstractSupportDefinition implements Iterable<ChartColumnValue>{
	
	/** 定义数据区域的列 */
	private final List<ChartColumnValue> columns = new ArrayList<>();
	/** 定义用于显示的统计图形 */
	private final List<ChartGraphic> graphics = new ArrayList<>();
	/** 定义查询区域的界面 */
	private String queryPath = "plugs-chart/index-query.vm";
	/** 默认查询项的定义 */
	private ChartColumnAxis axis;
	/** 从数据库查询数据 */
	private ChartValueReader reader;
	
	/** 数据区域所定义的头部(head)的最高列数，不包括axis部分 */
	private int rowspan;//这个值应该在执行的时候再分配
	private int pageSize = 100;
	private boolean exportable = true;
	
	public boolean isExportable() {
		return exportable;
	}
	
	public String getQueryPath() {
		return queryPath;
	}
	
	public ChartColumnAxis getAxis() {
		return axis;
	}
	
	@Override
	public void finish() {
		super.finish();
		Collections.sort(graphics);
	}
	
	@Override
	public Iterator<ChartColumnValue> iterator() {
		return columns.iterator();
	}
	
	public List<ChartGraphic> getGraphics() {
		return graphics;
	}
	
	public ChartDefinition(String key, String name) {
		super(key, name);
	}
	
	public void setExportable(boolean exportable) {
		this.exportable = exportable;
	}
	
	public void setQueryPath(String queryPath) {
		this.queryPath = queryPath;
	}
	
	public void setReader(ChartValueReader reader) {
		this.reader = reader.initialize(this);
	}
	
	public ChartValueReader getReader() {
		return reader;
	}
	
	/** 默认的统计项，也可以在具体执行查询的时候，根据条件自定义返回 */
	public void axis(ChartColumnAxis axis){ 
		this.axis = axis;
	}

	public void addColumn(ChartColumnValue column){
		columns.add(column);
		if(column.rowspan() > rowspan) rowspan = column.rowspan();
	}

	public ChartRow newRow() {
		ChartCell[] cells = new ChartCell[columns.size()];
		int i = 0;
		for(ChartColumn column : columns){
			cells[i++] = column.newCell();
		}
		return new ChartRow(cells);
	}
	
	public int getRowspan() {
		return rowspan;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	
	private ChartGraphic internalGraphic(ChartGraphic graphic){
		graphics.add(graphic);
		return graphic; 
	}
	
	public ChartGraphic graphic(String name, ChartGraphicType type){
		return graphic(name, type, ChartGraphicAxis.X);
	}
	
	public ChartGraphic graphic(String name, ChartGraphicType type, ChartGraphicAxis axis){
		return internalGraphic(new ChartGraphic(name, type, axis));
	}
}
